package com.ccExcelUtil.checkData;

import com.ccExcelUtil.validator.ValidatorUtils;

import javax.validation.ConstraintViolation;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author xiaozhao
 */
public class CheckDataUtil {

    public static <E> void checkData(E t, Class<?>... groups) {
        Set<ConstraintViolation<Object>> constraintViolations = ValidatorUtils.validateEntity(t, groups);
        if (!constraintViolations.isEmpty()) {
            ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
            throw new RuntimeException(constraint.getMessage());
        }
    }

    public static <E> Map<String, Object> checkDataToField(E t, Class<?>... groups) {
        return ValidatorUtils.getValidatedObject(t, groups);
    }


    public static <E> void multiCheckData(List<E> dataList, Class<?>... groups) {
        for (E t : dataList) {
            checkData(t, groups);
        }
    }
}
